#include "Logger.h" 
#include <bautils.H>
#include <pathinfo.h>
#include <COEUTILS.H>


_LIT(KLogFolder, "data\\");
_LIT(KLogFileExt, ".log");
_LIT(KOldFileExt, ".old");
 
const TUint8 KBackSlash = '\\';
_LIT8(KLineEnd, "\r\n");
 
const TInt KTimeRecordSize = 16;
 
_LIT8(KTimeFormat,"%02d:%02d:%02d:%06d ");
_LIT8(KLogStart, "--== New %S log ==--\r\n");
 
_LIT(KSquareBracket, "[");\

CLogger::CLogger()
{
}
 
TBool CLogger::Construct()
{
	TInt err = iFs.Connect();
	if (!err)
	{
		TFileName logFileName;
		err = !GetLogFileNameWithoutExt(logFileName);
		if(!err)
		{
			logFileName.Append(KLogFileExt);
			ConeUtils::EnsurePathExistsL(logFileName);
			err = iFile.Open(iFs, logFileName, EFileShareAny | EFileWrite);
			if(err != KErrNone)
			{
				err = iFile.Create(iFs, logFileName, EFileShareAny | EFileWrite);
			}

			TInt pos(0);
			iFile.Seek(ESeekEnd, pos);
			TBuf8<KMaxFullName> appName;
			appName.Copy(BaflUtils::ExtractAppNameFromFullName(RThread().FullName()));
			//iLogBuffer.AppendFormat(KLogStart, &appName);
			err = iFile.Write(iLogBuffer);
		
		}
	}
	return (!err);
}
 
CLogger* CLogger::Logger()
{
	//CLogger* logger = (CLogger*)Dll::Tls();
	//if (!logger)
	//{
	//	logger = new CLogger;
	//	if (logger)
	//	{
	//		if (logger->Construct())
	//		{
	//			Dll::SetTls(logger);
	//		}
	//		else
	//		{
	//			delete logger;
	//			logger = NULL;
	//		}
	//	}
	//}
	CLogger* logger = new CLogger;
	logger->Construct();
	return logger;
}
 
/*EXPORT_C*/ void CLogger::Close()
{
	//delete (CLogger*)Dll::Tls();
	//Dll::FreeTls();
}
 
CLogger::~CLogger()
{
	iFile.Close();
	iFs.Close();
}
 
TBool CLogger::GetLogFileNameWithoutExt(TDes& aFileName)
{
//#ifndef _DEBUG
//	aFileName.Copy(PathInfo::MemoryCardRootPath());
//#else
//	TChar drive;
//	iFs.DriveToChar(EDriveC, drive);
//	aFileName.Append(drive);
//	const TUint8 KColon = ':';
//	aFileName.Append(KColon);
//	aFileName.Append(KBackSlash);
//#endif
//	aFileName.Append(KLogFolder);
//	TBool res = BaflUtils::FolderExists(iFs, aFileName);
//	if (!res)
//	{
//		aFileName.Zero();
//	}
//	else
//	{
//		TPtrC fileName(BaflUtils::ExtractAppNameFromFullName(RThread().FullName()));
//		TPtrC procName(RProcess().FullName());
//		TPtrC folderName(TParsePtrC(procName.Left(procName.Find(KSquareBracket))).Name());
//		aFileName.Append(folderName);
//		aFileName.Append(KBackSlash);
//		aFileName.Append(fileName);
//	}
//
//	return res;
			
//#ifdef __SERIES60_3X__
//	aFileName.Copy(_L("c:\\data\\"));
//	TPtrC fileName(BaflUtils::ExtractAppNameFromFullName(RThread().FullName()));
//	TPtrC procName(RProcess().FullName());
//	TPtrC folderName(TParsePtrC(procName.Left(procName.Find(KSquareBracket))).Name());
//	aFileName.Append(folderName);
//	aFileName.Append(KBackSlash);
//	aFileName.Append(fileName);
//#else
	aFileName.Copy(_L("c:\\data\\eStock_L2"));
//#endif
	return ETrue;
}
 
/*EXPORT_C*/ void CLogger::Write(const TDesC8& aText)
{
	CLogger* logger = Logger();
	if(logger)
	{
		logger->iLogBuffer.Copy(aText);
		logger->DoWrite();
		delete logger;
	}
}
 
/*EXPORT_C*/ void CLogger::Write(const TDesC16& aText)
{
	CLogger* logger = Logger();
	if(logger)
	{
		logger->iLogBuffer.Copy(aText);
		logger->DoWrite();
		delete logger;
	}
}
 
/*EXPORT_C*/ void CLogger::Write(TRefByValue<const TDesC8> aFmt,...)
{
	CLogger* logger = Logger();
	if (logger)
	{
		VA_LIST list;
		VA_START(list, aFmt);
		logger->iLogBuffer.FormatList(aFmt, list);
		logger->DoWrite();
		VA_END(list);
		delete logger;
	}
}
 
void CLogger::DoWrite()
{
	if(iFile.SubSessionHandle())
	{
		TTime time;
		time.HomeTime();
		TDateTime dateTime;
		dateTime = time.DateTime();
		TBuf8<KTimeRecordSize> timeRecord;
		timeRecord.Format(KTimeFormat, dateTime.Hour(), dateTime.Minute(), dateTime.Second(), dateTime.MicroSecond());
		iLogBuffer.Insert(0, timeRecord);
		iLogBuffer.Append(KLineEnd);
		iFile.Write(iLogBuffer);
#ifdef _DEBUG
		iFile.Flush();
#endif // _DEBUG
	}
}
